package String;

public class _443_StringCompression {
    public int compress(char[] chars) {
        String lastString = "";
        char prev = chars[0];
        int count = 1;
        for (int i=1;i<chars.length;i++) {
            if (chars[i] == prev) {
                count++;
            } else {
                if (count == 1) {
                    lastString += prev;
                } else {
                    lastString += prev;
                    lastString +=count;
                }

                count = 1;
                prev = chars[i];
            }
        }
        if (count == 1) {
            lastString += chars[chars.length-1];
        } else {
            lastString += chars[chars.length-1];
            lastString += count;
        }
        for(int i=0;i<lastString.length();i++) {
            chars[i] = lastString.charAt(i);
        }
        return lastString.length();
    }
}
